package cn.wuxuan.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.gzsxt.utils.DBUtil;
import cn.wuxuan.dao.UserDao;
import cn.wuxuan.po.QueryObject;
import cn.wuxuan.po.Result;
import cn.wuxuan.po.Task;
import cn.wuxuan.po.TaskTrans;
import cn.wuxuan.po.User;
import cn.wuxuan.util.BaseDao;
import cn.wuxuan.util.BaseDaoImpl;

public class UserDaoImpl implements UserDao {
	User user = null;
	BaseDao baseDao = new BaseDaoImpl();
	Connection conn = DBUtil.getConn();
	@Override
	public User selectName(String name) {
		String sql = "select * from t_user where name = '"+name+"'";
			user = baseDao.selectSingleRow(User.class, sql);
		return user;
	}

	@Override
	public Integer verifyUser(User u) {
		Integer count=0;
		String name = u.getName();
		user = selectName(name);
		if(user!=null&&u.getPwd().equals(user.getPwd())){
				count = 1;
		}
		return count;
	}

	@Override
	public Integer checkTitle(String title) {
		String sql = "select * from t_taskwx where title = '"+title+"'";
		Task task = baseDao.selectSingleRow(Task.class, sql);
		Integer count=0;
		
		if(task!=null&&title.equals(task.getTitle())){
				count = 1;
		}
		return count;
	}

	@Override
	public Integer addTask(Task task) {
			String sql1 = "insert into t_taskwx (title,content,annex,status,urgent,person,dept,createtime,strtime,fintime) values"
					+ "(?,?,?,?,?,?,?,?,?,?)";
			String sql2 = "insert into t_trans (title,content,transfintime) values"
					+ "(?,?,?)";
				Object[] params1 = {task.getTitle(),task.getContent(),task.getAnnex(),
					task.getStatus(),task.getUrgent(),task.getPerson(),task.getDept(),
					task.getCreatetime(),task.getStrtime(),task.getFintime()}; 
				Object[] params2 = {task.getTitle(),task.getContent(),task.getFintime()}; 
					Integer count = baseDao.update(sql1, params1);
					count += baseDao.update(sql2, params2);
						return count;
	}

	@Override
	public Integer findTotalCount(QueryObject qo) {
		String keyWord = qo.getKeyWord();
		String sql = "select count(*) from t_trans";
		if(keyWord!=null&&!"".equals(keyWord.trim())){
			sql= "select count(*) from t_trans  where title like '%"+keyWord+"%' or delayclerk like '%"+keyWord+"%'" ;
		}
		Integer count = null;
		try {
			PreparedStatement prest = conn.prepareStatement(sql);
			ResultSet set = prest.executeQuery();
			if(set.next()){
				count = set.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<TaskTrans> selectTaskTrans(QueryObject qo) {
		Integer begin = qo.getBeginIdex();
		String keyWord = qo.getKeyWord();
		Integer pageSize = qo.getPageSize();
		String sql = null;
		if(keyWord!=null&&!"".equals(keyWord.trim())){
			sql="select * from t_taskwx,t_trans where t_taskwx.title=t_trans.title"
					+ " and t_trans.title like '%"+keyWord+"%' or t_trans.delayclerk like '%"+keyWord+"%' limit ?,?";
		}else {
			sql= "select * from t_trans ,t_taskwx where t_taskwx.title=t_trans.title  limit ?,?";
		}
		List<TaskTrans> list = baseDao.selectListRow(TaskTrans.class, sql, begin,pageSize);
	
		return list;
	}

	@Override
	public Integer deleteTask(String title) {
		String sql1 = "delete from t_trans where title = ?";
		String sql2 = "delete from t_taskwx where title = ?";
		Integer count = baseDao.update(sql1, title);
		count += baseDao.update(sql2, title);
		return count;
	}

	@Override
	public Integer changeTask(TaskTrans trans) {
		String title = trans.getTitle();
		String applyMan = trans.getDelayclerk();
		String reson = trans.getTransreason();
		Integer status = trans.getTransstatus();
		String acceptor =trans.getAcceptor();
		String opinion = trans.getTransopinion();
		String sql = "update  t_trans set delayclerk = ?,transreason = ?, "
				+ "delaystatus = ?,acceptor = ?,transopinion = ? where title = '"+title+"'";
		Object[] params = {applyMan,reson, status,acceptor,opinion}; 
		Integer count = baseDao.update(sql, params);
		return count;
	}

	@Override
	public List<Result> selectResult(QueryObject qo) {
			Integer begin = qo.getBeginIdex();
			String keyWord = qo.getKeyWord();
			Integer pageSize = qo.getPageSize();
			String sql = null;
			if(keyWord!=null&&!"".equals(keyWord.trim())){
				sql="select * from t_taskwx where "
						+ " t_taskwx.title like '%"+keyWord+"%' or t_taskwx.person like '%"+keyWord+"%' limit ?,?";
			}else {
				sql= "select * from t_taskwx  limit ?,?";
			}
			List<Result> list = baseDao.selectListRow(Result.class, sql, begin,pageSize);
			return list;
	}

	@Override
	public Integer changeResult(Result result) {
		String title = result.getTitle();
		String achieve = result.getAchieve();
		String achievefile = result.getAchievefile();
		String sql = "update  t_taskwx set achieve = ?,achievefile = ?, "
				+ " where title = '"+title+"'";
		Object[] params = {achieve,achievefile}; 
		Integer count = baseDao.update(sql, params);
		return count;
	}

}
